package com.lz.service.smsmail.common;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * request 请求上下文  线程本地
 * @author likejian
 *
 */
public class RequestContext {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(RequestContext.class);
	private final static ThreadLocal<RequestContext> REQUEST_CONTEXT = new ThreadLocal<RequestContext>();
	private HttpServletRequest request;
	private HttpServletResponse response;
	//请求时间
	private Long requestTime;
	//响应时间
	private Long responseTime;
	private String uri;
	private String method;
	private String remoteAddr;
	
	
	/**
	 * 初始化请求上下文
	 * @param ctx
	 * @param request
	 * @param response
	 */
	public static RequestContext begin(HttpServletRequest request, HttpServletResponse response) {
		RequestContext requestContext = new RequestContext();
		requestContext.request = request;
		requestContext.response = response;
		String uri = request.getRequestURI().replace(request.getContextPath(), "");
		String remoteAddr = getIpAddr(request);
		requestContext.uri = uri;
		requestContext.requestTime = System.currentTimeMillis();
		requestContext.remoteAddr = remoteAddr;
		REQUEST_CONTEXT.set(requestContext);
		return requestContext;
	}
	
	/**
	 * 获取当前请求的上下文
	 * @return
	 */
	public static RequestContext get(){
		return REQUEST_CONTEXT.get();
	}

	/**
	 * 结束请求
	 * @param LOGGER
	 * @param responseConent	响应内容
	 * @throws IOException
	 */
	public static void end(String responseConent){
		RequestContext requestContext = RequestContext.get();
		//请求时间
		Long responseTime = System.currentTimeMillis();
		//响应时间
		Long requestTime = requestContext.getRequestTime();
		if(responseTime != null && requestTime != null){
			//响应时间
			Long useTime = responseTime - requestTime;
			LOGGER.info("uri={} use_time={} conent={} end", requestContext.getUri(), useTime, responseConent);
		}
		REQUEST_CONTEXT.remove();
		return;
	}
	
	/**
	 * 错误日志记录
	 * @param LOG
	 * @param e
	 */
	public static void exceptionLog(Exception e){
		LOGGER.error("exception={}" , ExceptionUtils.getFullStackTrace(e));
	}
	
	 /**
     * 获取登录用户远程主机ip地址
     * 
     * @param request
     * @return
     */
    private static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
    
	public String getMethod() {
		return method;
	}

	public void setMethod(String method) {
		this.method = method;
	}

	public String getRemoteAddr() {
		return remoteAddr;
	}

	public void setRemoteAddr(String remoteAddr) {
		this.remoteAddr = remoteAddr;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	public String getUri() {
		return uri;
	}

	public void setUri(String uri) {
		this.uri = uri;
	}

	public Long getRequestTime() {
		return requestTime;
	}

	public void setRequestTime(Long requestTime) {
		this.requestTime = requestTime;
	}

	public Long getResponseTime() {
		return responseTime;
	}

	public void setResponseTime(Long responseTime) {
		this.responseTime = responseTime;
	}

}
